package com.resource.jwt;

import com.redis.RedisUtil;
import com.resource.base.read.BaseDoing;
import com.resource.base.BaseDto;
import com.utility.constant.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 令牌增强实现类
 *  实现自定义参数添加
 * @author: hw
 * @date: 2020/12/9 16:57
 */
public class CustomTokenEnhancer implements TokenEnhancer{

    @Autowired private RedisUtil redisUtil;

    @Autowired
    private BaseDoing baseDoing;


    @Override
    public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

        BaseDto baseDto = baseDoing.getDoingBase();
        baseDto.setToken(accessToken.getValue());
        redisUtil.set(authentication.getName(),baseDto, Constant.TOKEN_VALID);
        /**
         * 自定义参数
         */
        Map<String, Object> additionalInformation = new LinkedHashMap<>();
        Map<String, Object> info = new LinkedHashMap<>();
//        info.put("custom","自定义");
//        additionalInformation.put("info",info);
        additionalInformation.put("result", "200");
        ((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(additionalInformation);
        return accessToken;
    }

}
